#include "drawform.h"
#include "ui_drawform.h"
#include <QMouseEvent>
#include <QPainter>
#include <QtMath>


DrawForm::DrawForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::DrawForm)
    , mDrawType(0)
{
    ui->setupUi(this);

}

DrawForm::~DrawForm()
{
    delete ui;
}

bool isFirst = true;
QPoint temppt;

void DrawForm::mousePressEvent(QMouseEvent *event)
{

    if(mDrawType == 1)
    {
        DwgLine *line = new DwgLine ;
        if(isFirst)
        {
            temppt =event->pos();
            isFirst = false;
        }
        else
        {
            line->mStartPt = temppt;
            line->mEnd  = event->pos();
            MainList.append(line);
            isFirst = true;
            update();
        }
    }
    if(mDrawType == 2)
    {
        DwgCircle *circle = new DwgCircle;
        if(isFirst)
        {
            temppt = event->pos();
            isFirst = false;
        }
        else
        {
            circle->mCenterPt = temppt;
            circle->mRad = qSqrt((temppt.x()-event->pos().x())*(temppt.x()-event->pos().x())+(temppt.y()-event->pos().y())*(temppt.y()-event->pos().y()));
            MainList.append(circle);
            isFirst = true;
            update();
        }
    }
    if(mDrawType=3)
    {
        DwgRec *rec =new DwgRec;
        if(isFirst)
        {
            temppt =event->pos();
            isFirst = false;
        }
        else
        {
            rec->mLUPt = temppt;
            rec->mRDPt = event->pos();
            MainList.append(rec);
            isFirst = true;
            update();
        }
    }
    if(mDrawType=4)
    {
        DwgArc *arc =new DwgArc;
        if(isFirst)
        {
            temppt =event->pos();
            isFirst = false;
        }
        if(isScend)
        {
            arc->mcenterPt = temppt;
            arc->mstartPt =event->pos();
            arc->mrad = qSqrt((temppt.x()-event->pos().x())*(temppt.x()-event->pos().x())+(temppt.y()-event->pos().y())*(temppt.y()-event->pos().y()));
            arc->ma = atan((event->pos().y()-temppt.y())/(event->pos().x()-temppt.x()));
            isFirst = false;
        }
        else
        {
            arc->mendPt=event->pos();
            arc->mb= atan((event->pos().y()-arc->mendPt.y())/(event->pos().x()-mendPt.x()))-arc->ma;
            MainList.append(circle);
            isFirst = true;
            update();
        }

    }


    }
}


void DrawForm::paintEvent(QPaintEvent *event)
{
        painter = new QPainter(this);
        QPen pen;
          pen.setColor(Qt::red);
          pen.setWidth(3);
          painter->setPen(pen);

          for(auto i :MainList)
                i->Draw(painter);


}

void DrawForm::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_1)
    {   mDrawType = 1;      ui->label->setText("开始画线");}
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_2)
    {   mDrawType = 2;  ui->label->setText("开始画圆");}
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_3)
    {   mDrawType = 3;  ui->label->setText("开始画矩形");}
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_4)
    {   mDrawType = 4;  ui->label->setText("开始画弧");}
    this->setCursor(Qt::CrossCursor);
}

